let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    defaultSrc: '/images/mine/mine@2x.png',
    avatarUrl: '',
    oldAvatar: '',
    photoList: [
      '/images/avatar/1.png',
      '/images/avatar/2.png',
      '/images/avatar/3.png',
      '/images/avatar/4.png',
      '/images/avatar/5.png',
      '/images/avatar/6.png',
      '/images/avatar/7.png',
      '/images/avatar/8.png',
      '/images/avatar/9.png',
      '/images/avatar/10.png',
      '/images/avatar/11.png',
      '/images/avatar/12.png',
      '/images/avatar/13.png',
      '/images/avatar/14.png',
      '/images/avatar/15.png',
      '/images/avatar/16.png',
      '/images/avatar/17.png',
      '/images/avatar/18.png',
      '/images/avatar/19.png',
      '/images/avatar/20.png',
      '/images/avatar/21.png',
      '/images/avatar/22.png',
      '/images/avatar/23.png',
      '/images/avatar/24.png',
      '/images/avatar/25.png',
      '/images/avatar/26.png',
      '/images/avatar/27.png',
      '/images/avatar/28.png'
    ],
    currentPage: 0,
    currentList: []
  },
  toggleDot(e){
    this.setData({
      currentPage: e.detail.current
    })
  },
  togglePhoto(e){
    this.setData({
      avatarUrl: e.target.dataset.item
    })
  },
  finish(){
    if (this.data.avatarUrl != this.data.oldAvatar){
      let _this = this
      let getUserInfo = wx.getStorageSync("userInfo")
      wx.request({
        url: app.globalData.baseUrl + '/WXupdateInfo',
        data: {
          cluesOrStuId: getUserInfo.cluesOrStuId,
          type: getUserInfo.type,
          info: {
            'photo': _this.data.avatarUrl
          }
        },
        method: 'post',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: '保存成功',
              image: '/images/register/chenggong@2x.png'
            })
            _this.setData({
              userInfo: res.data.data.info
            })
            Object.assign(app.globalData.userInfo, res.data.data.info)
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.showToast({
              title: '保存失败',
              image: '/images/register/cuowu@2x.png'
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    let list = [];
    let list1 = this.data.photoList.slice(0, 16);
    let list2 = this.data.photoList.slice(16);
    list.push(list1)
    list.push(list2)
    this.setData({
      currentList: list,
      avatarUrl: this.data.userInfo.photo ? this.data.userInfo.photo : this.data.defaultSrc,
      oldAvatar: this.data.userInfo.photo ? this.data.userInfo.photo : this.data.defaultSrc
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})